package com.baihui.crm.export;

import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import ognl.Ognl;
import ognl.OgnlException;

import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jsoup.Jsoup;
import org.xml.sax.InputSource;

import com.baihui.crm.BaihuiResponse;
import com.baihui.crm.BaihuiSession;
import com.baihui.crm.util.BaihuiException;
import com.baihui.crm.util.BaihuiOptUtil;
import com.baihui.taobao.authorize.TaoBaoSessionInfo;
import com.baihui.taobao.db.CommonDAO;
import com.baihui.taobao.db.GeneDAO;
import com.baihui.taobao.db.UserDAO;
import com.baihui.taobao.util.Util;
import com.baihui.taobao.vo.BUser;

/**
 * Created by Kevin. User: HSZM1106-0312 Date: 12-1-16 Time: 下午10:24
 */
public class ExportData {

	protected Logger logger = Logger.getLogger(getClass());

	public static final String Contacts = "Contacts";
	public static final String Leads = "Leads";
	public static final String Orders = "SalesOrders";
	public static final String Products = "Products";

	protected BaihuiSession baihuiSession;
	protected TaoBaoSessionInfo taobaosession;
	protected Properties loader;

	public ExportData(BaihuiSession baihuiSession,
			TaoBaoSessionInfo taoBaoSessionInfo) {
		this.baihuiSession = baihuiSession;
		this.taobaosession = taoBaoSessionInfo;
	}

	public void log(String info) {
		// System.out.println(info);
		logger.info(info);
	}

	/**
	 * 导出数据到百会
	 * 
	 * @param type
	 * @param userid
	 *            专门有一个表存每个模块导出数据的最后时间. 每个模块增加字段是否已经导出.
	 */
	protected List<Map> exportData(String type, Long userid) throws Exception {
		// 如果不存在百会Session，返回Null
		if (baihuiSession == null) {
			logger.info("百会Session为空！");
			return null;
		}
		// 查找需要导出的数据
		List<Map> allData = findExportData(type, userid);
		if (allData == null || allData.size() <= 0)
			return null;
		// 循环形成XMLDataString
		int index = 1, exportCount = 0, successCount = 0;

		// 形成的XMLDataString每隔5条记录提交一次.
		int count = 100, startIndex = 0;
		// 如果导入类型为订单时，由于zoho插入接口问题一次只能插入一条记录。
		if ("SalesOrders".equals(type)) {
			count = 1;
		}
		List<Map> subList = Util.subList(allData, startIndex, count);

		while (subList != null && subList.size() > 0) {
			try {
				StringBuilder stringBuilder = new StringBuilder();
				// 批量形成数据.
				stringBuilder.append("<" + type + ">");
				for (Map map : subList) {

					// 根据配置得到百会字段与淘宝字段的映射
					String xmlDataString = populate(map, type, index);
					// 调用导出数据，返回是否导出成功.
					if (xmlDataString == null) {
						logger.info("获取的XMLString为空！");
						continue;
					}

					// 如果数据的baihuiid 不为空，说明是更新的数据
					if (map.get("baihuiid") != null
							&& !"".equals(map.get("baihuiid"))) {
						xmlDataString = "<" + type + ">" + xmlDataString + "</"
								+ type + ">";
						BaihuiOptUtil.updateRecord(baihuiSession, type,
								xmlDataString, map.get("baihuiid").toString());

						// 将更新过的订单数据导出标识设为1
						UserDAO userDAO = new UserDAO();
						String updateString = "update tbtrade set exported = 1 where userId = ? and tid = ? ";
						userDAO.updateAndInsert(updateString, new Object[] {
								map.get("userId"), map.get("tid") });

					} else {
						if (!xmlDataString
								.contains("<FL val=\"Product Id\">null</FL>")) {
							stringBuilder.append(xmlDataString);
							exportCount++;
						}
					}
					index++;
				}
				stringBuilder.append("</" + type + ">");
				// System.out.println("stringBuilder ================" +
				// (stringBuilder==null?null:stringBuilder.toString()));
				String result = null;
				String selectResult = null;
				// if (subList.get(0).get("baihuiid") != null) {
				// result = BaihuiOptUtil.updateRecord(baihuiSession, type,
				// stringBuilder.toString(),
				// subList.get(0).get("baihuiid").toString());
				// } else {
				if (!("<" + type + "></" + type + ">").equals(stringBuilder
						.toString())) {
					Date date = new Date();
					result = BaihuiOptUtil.insertRecord(baihuiSession,
							taobaosession, type, stringBuilder.toString());
					List<BaihuiResponse> ret = null;
					if (type.equals("SalesOrders")) {
						for (Map map : subList) {
							// System.out.println(userid);
							// System.out.print(map.get("tid")+",");
							try {
								ret = parse(userid, map.get("tid") + "", result);
							} catch (Exception e) {
								System.out.println("用户：" + userid + " result ======" + result);
								log("用户：" + userid + "的同步出错数据" + stringBuilder.toString());
								throw e;
							}
						}
					} else {
						selectResult = BaihuiOptUtil.selectRecord(
								baihuiSession, type, date);
						// 解析结果.
						ret = parse(result, selectResult, subList, type);
					}
					if (ret != null && ret.size() > 0) {
						if ("".equals(ret.get(0).getErrCode())) {
							List<String> updateBatch = new ArrayList<String>();
							for (BaihuiResponse baihuiResponse : ret) {
								successCount++;
								String[] sql = getUpdateSQL(
										baihuiResponse.getBaihuiId(),
										baihuiResponse.getTaobaoId(), type,
										baihuiResponse.getTaobaoObject());
								if (sql != null && sql.length > 0) {
									for (String tmp : sql) {
										updateBatch.add(tmp);
									}
								}
							}

							// 更新记录
							if (updateBatch != null && updateBatch.size() > 0) {
								update(updateBatch, null, userid, type);
							}
						} else {
							log("用户【" + userid + "】" + stringBuilder.toString()
									+ " 同步到百会时失败！");
							BaihuiException ex = new BaihuiException();
							ex.setErrCode(ret.get(0).getErrCode());
							throw ex;
						}
					}

				}
				// selectResult = BaihuiOptUtil.selectRecord(baihuiSession,
				// type, date);
				// }
				startIndex = startIndex + count;
				subList = Util.subList(allData, startIndex, count);
				index = 1;
			} catch (Exception ex) {
				throw ex;
			}
		}
		afterExported(type, userid);
		log(userid + "计划导出" + type + exportCount + "条，实际" + successCount + "条");
		long end3 = System.currentTimeMillis();
		// logger.info("导出数据总用时" + (end3 - start) + "豪秒");
		return allData;
	}

	protected void afterExported(String type, Long userid) throws Exception {
		// 更新商品的总数量
	}

	/**
	 * 更新
	 * 
	 * @param updateBatch
	 * @param cn
	 * @param userid
	 * @param type
	 */
	private void update(List<String> updateBatch, Connection cn, Long userid,
			String type) throws Exception {
		// 更新updateBatch
		CommonDAO commonDAO = new CommonDAO();
		commonDAO.updateAndInsert(updateBatch, null);
		// 增加或者更新该模块已经导出的最大时间.
	}

	/**
	 * 获取需要导出的数据，如果不满足，可以继承这个类来拼MAP内容。
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<Map> findExportData(String type, Long userid) throws Exception {
		// 根据USERID查询未导出的数据，并查询最后的导出数据时间
		String sql = getSql(type, userid);
		if (sql == null || "".equals(sql)) {
			logger.info("无法获取查询未导出数据的SQL语句！");
			return null;
		}
		// 查询本次可以导出的数据量
		GeneDAO geneDAO = getDomainDAO();
		List<Map> allData = geneDAO.findAllObject(sql, new Object[] {});
		// logger.info("用户" + userid + "对应的 " + type + "类型的数据条数为：" +
		// allData.size());
		allData = constructureData(allData, type, userid);
		return allData;
	}

	/**
	 * 组装数据
	 * 
	 * @param allData
	 * @param type
	 * @param userid
	 * @return
	 */
	protected List<Map> constructureData(List<Map> allData, String type,
			Long userid) throws Exception {
		return allData;
	}

	/**
	 * @param response
	 * @return
	 * @throws IOException
	 * @throws JDOMException
	 */

	public List<BaihuiResponse> parse(Long userId, String taobaoid,
			String response) throws JDOMException, IOException {
		List<BaihuiResponse> parseRet = new ArrayList<BaihuiResponse>();
		BaihuiResponse baihuiResponse = new BaihuiResponse();
		StringReader reader = new StringReader(response);
		org.xml.sax.InputSource inputSource = new InputSource(reader);
		SAXBuilder sb = new SAXBuilder();
		Document doc = sb.build(inputSource);
		Element root = doc.getRootElement();
		Element recorddetail = root.getChild("result").getChild("recorddetail");
		boolean isSuccess = root.getChild("result").getText()
				.indexOf("successfully") > -1;
		List<Element> subElem = recorddetail.getChildren("FL");
		baihuiResponse.setSuccess(isSuccess);
		baihuiResponse.setErrCode("");
		Map<String, String> map = new HashMap<String, String>();
		map.put("userId", userId + "");
		baihuiResponse.setTaobaoObject(map);
		baihuiResponse.setTaobaoId(taobaoid);
		for (Element tmp : subElem) {
			String attrValue = tmp.getAttributeValue("val");
			if (attrValue != null) {
				if (attrValue.equals("Id")) {
					baihuiResponse.setBaihuiId(tmp.getText());
				}
				if (attrValue.equals("Created Time")) {
					baihuiResponse.setCreateTime(Timestamp.valueOf(tmp
							.getText()));
				} else if (attrValue.equals("Modified Time")) {
					baihuiResponse.setModifyTime(Timestamp.valueOf(tmp
							.getText()));
				} else if (attrValue.equals("Created By")) {
					baihuiResponse.setCreateBy(tmp.getText());
				} else if (attrValue.equals("Modified By")) {
					baihuiResponse.setModifyBy(tmp.getText());
				}
			}
		}
		parseRet.add(baihuiResponse);
		return parseRet;
	}

	/**
	 * 解析结果，返回成功或者失败.
	 * 
	 * @param response
	 * @return
	 */
	protected List<BaihuiResponse> parse(String response,
			String selectResponse, List<Map> subList, String type)
			throws Exception {
		if (response == null || response.length() <= 0 || subList == null
				|| subList.size() <= 0)
			return null;
		List<BaihuiResponse> parseRet = new ArrayList<BaihuiResponse>();
		// 是插入的情况
		Map map = new HashMap();
		if (selectResponse != null && !"".equals(selectResponse)) {
			BaihuiResponse baihuiResponse = new BaihuiResponse();
			StringReader reader = new StringReader(selectResponse);
			org.xml.sax.InputSource inputSource = new InputSource(reader);
			SAXBuilder sb = new SAXBuilder();
			Document doc = sb.build(inputSource);
			Element root = doc.getRootElement();
			Element error = root.getChild("error");
			if (error == null) {
				error = root.getChild("nodata");
			}
			if (error != null) {
				// System.out.println(response);
				Element code = error.getChild("code");
				Element msg = error.getChild("message");
				log("调用百会接口时失败，错误号为" + code.getText() + ",错误信息为:"
						+ msg.getText());
				baihuiResponse.setErrCode(code.getText());
				parseRet.add(baihuiResponse);
				return parseRet;
			}
			map = getMapByDoc(doc, type);
		}
		try {
			StringReader reader = new StringReader(response);
			BaihuiResponse baihuiResponse = new BaihuiResponse();
			org.xml.sax.InputSource inputSource = new InputSource(reader);
			SAXBuilder sb = new SAXBuilder();
			Document doc = sb.build(inputSource);
			// 得到根元素和Result元素.
			Element root = doc.getRootElement();
			Element ret = root.getChild("result");
			Element error = root.getChild("error");
			if (error != null) {
				// System.out.println(response);
				Element code = error.getChild("code");
				Element msg = error.getChild("message");
				log("调用百会接口时失败11，错误号为" + code.getText() + ",错误信息为:"
						+ msg.getText());
				baihuiResponse.setErrCode(code.getText());
				parseRet.add(baihuiResponse);
				return parseRet;
			}
			if (ret != null) {
				// 得到Message信息
				Element message = ret.getChild("message");
				boolean isSuccess = message.getText().indexOf("successfully") > -1;
				// 得到所有的recordDetail
				List children = ret.getChildren("recorddetail");
				Element et = null;
				for (int i = 0; i < children.size(); i++) {
					baihuiResponse = new BaihuiResponse();
					et = (Element) children.get(i);
					baihuiResponse.setSuccess(isSuccess);
					baihuiResponse.setErrCode("");
					baihuiResponse
							.setTaobaoId(getTaoBaoId(subList.get(i), type));
					baihuiResponse.setTaobaoObject(subList.get(i));
					if ("Products".equals(type) || "SalesOrders".equals(type)) {
						if (map.get(String.valueOf(baihuiResponse.getTaobaoId())) == null) {
							continue;
						}
						baihuiResponse.setBaihuiId(String.valueOf(map
								.get(String.valueOf(baihuiResponse
										.getTaobaoId()))));
					} else if ("Contacts".equals(type)) {
						if (map.get(String.valueOf(subList.get(i).get("nick"))) == null) {
							continue;
						}
						baihuiResponse
								.setBaihuiId(String.valueOf(map.get(String
										.valueOf(subList.get(i).get("nick")))));
					}

					List<Element> subElem = et.getChildren("FL");
					for (Element tmp : subElem) {
						String attrValue = tmp.getAttributeValue("val");
						if (attrValue != null) {
							// if (attrValue.equals("Id")) {
							// baihuiResponse.setBaihuiId(tmp.getText());
							// }
							if (attrValue.equals("Created Time")) {
								baihuiResponse.setCreateTime(Timestamp
										.valueOf(tmp.getText()));
							} else if (attrValue.equals("Modified Time")) {
								baihuiResponse.setModifyTime(Timestamp
										.valueOf(tmp.getText()));
							} else if (attrValue.equals("Created By")) {
								baihuiResponse.setCreateBy(tmp.getText());
							} else if (attrValue.equals("Modified By")) {
								baihuiResponse.setModifyBy(tmp.getText());
							}
						}
					}
					parseRet.add(baihuiResponse);
				}
			}
		} catch (JDOMException e) {
			throw new Exception(e);
		} catch (IOException e) {
			throw new Exception(e);
		}
		return parseRet;
	}

	/**
	 * 根据字段映射和类型形成XML字符串.
	 * 
	 * @param map
	 * @param type
	 * @return
	 */
	protected String populate(Map map, String type, int index) throws Exception {
		if (map == null || map.size() <= 0)
			return null;
		if (loader == null) {
			loader = new Properties();
			try {
				loader.load(this.getClass().getResourceAsStream(
						type + "_cn.properties"));
			} catch (IOException e) {
				throw new Exception(e);
			}
		}
		if (loader != null) {
			return constructureXMLString(map, index);
		}
		return null;
	}

	/**
	 * 构造XML字符串,此方法只支持简单的构造，复杂的请继承来做。
	 * 
	 * @param source
	 * @param index
	 * @return
	 * @throws OgnlException
	 */
	protected String constructureXMLString(Map source, int index)
			throws Exception {
		StringBuilder xmlDataStr = new StringBuilder();
		xmlDataStr.append("<row no='" + index + "'>");
		Set set = loader.keySet();
		// 处理除分录意外的所有内容.
		String[] entryProps = getEntryKeys();

		Map<String, List> listMap = new HashMap<String, List>();
		// long start = System.currentTimeMillis();
		for (Object o : set) {
			// long start2 = System.currentTimeMillis();
			String key = (String) o; // key对应百会字段
			if (key != null && key.length() > 0) {
				// 如果是分录条目，先保存下来.
				boolean handle = true;
				if (entryProps != null && entryProps.length > 0) {
					for (String entryKey : entryProps) {
						if (key.startsWith(entryKey)) {
							List tmp = listMap.get(entryKey);
							if (tmp == null)
								tmp = new ArrayList();
							tmp.add(key);
							listMap.put(entryKey, tmp);
							handle = false;
							break;
						}
					}
				}
				if (!handle)
					continue;
				// 处理非分录的条目
				String expression = (String) loader.getProperty(key); // expression是一个ongl表达式，一般为对象的属性
				if (expression == null || expression.equals(""))
					continue;
				expression = parseExpression(expression, key, source);
				Object value = getValue(expression, source);
				value = format(value, key);
				xmlDataStr.append("<FL val=\"" + getKey(key) + "\">" + value
						+ "</FL>");
			}
			// long Orderend2 = System.currentTimeMillis();
			// logger.info("组装订单中非分录的条目中的" + getKey(key) + "时用时"+ (Orderend2 -
			// start2) + "豪秒");
		}
		// long Orderend = System.currentTimeMillis();
		// logger.info("组装订单中非分录的条目" + xmlDataStr + "用时"+ (Orderend - start) +
		// "豪秒");
		// 处理分录的情况
		// long start1 = System.currentTimeMillis();
		if (entryProps != null && entryProps.length > 0) {
			for (String entryKey : entryProps) {
				// 得到所有分录的Key
				List keys = listMap.get(entryKey);
				// 得到对应的值
				String entryValueKey = loader.getProperty(entryKey);
				// 处理分录的情况
				String entryXmlDataStr = constructureEntryXMLString(keys,
						source, entryKey);
				if (entryXmlDataStr != null && entryXmlDataStr.length() > 0) {
					xmlDataStr.append(entryXmlDataStr);
				}
			}
		}
		// long Orderend1 = System.currentTimeMillis();
		// logger.info("组装订单中分录的情况" + xmlDataStr +"用时" + (Orderend1 - start1) +
		// "豪秒");
		afterConstructureXMLString(xmlDataStr, source, set,
				taobaosession.getVisitor_id());
		xmlDataStr.append("<FL val=\"来源\">" + "淘宝" + "</FL>");
		xmlDataStr.append("</row>");
		return xmlDataStr.toString();
	}

	/**
	 * 计算相关属性
	 * 
	 * @param xmlDataStr
	 * @param source
	 * @param set
	 */
	protected void afterConstructureXMLString(StringBuilder xmlDataStr,
			Map source, Set set, long userid) throws Exception {

	}

	protected String parseExpression(String expression, String key, Map source) {
		return expression;
	}

	/**
	 * expression如果以||分开，则分别取值，返回某个值.
	 * 
	 * @param expressioin
	 * @param source
	 * @return
	 * @throws Exception
	 */
	protected Object getValue(String expressioin, Map source) throws Exception {
		Map map = new HashMap();
		try {
			map.put("session", this.taobaosession);
			if (expressioin.indexOf("~") > -1) {
				String[] args = expressioin.split("~");
				for (String arg : args) {
					Object tmp = Ognl.getValue(arg, map, source);
					if (tmp != null)
						return tmp;
				}
				return null;
			} else {
				return Ognl.getValue(expressioin, map, source);
			}
		} catch (OgnlException e) {
			throw new Exception(e);
		}
	}

	/**
	 * 构造子分录的XML字符串
	 * 
	 * @param keys
	 * @param source
	 * @param entryKey
	 * @return
	 */
	protected String constructureEntryXMLString(List keys, Map source,
			String entryKey) throws Exception {
		return null;
	}

	/**
	 * 格式化值
	 * 
	 * @param value
	 * @param baihuiField
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	protected String format(Object value, String baihuiField)
			throws UnsupportedEncodingException {
		if (value == null)
			return null;
		// 存在格式化配置
		if (value instanceof Timestamp) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			return simpleDateFormat.format((Timestamp) value);
		} else if (value instanceof Date) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"MM/dd/yyyy");
			return simpleDateFormat.format((Date) value);
		} else if (value instanceof Integer) {
			return String.valueOf(((Integer) value).intValue());
		} else if (value instanceof Long) {
			return String.valueOf(((Long) value).longValue()); // TODO
		} else if (value instanceof BigDecimal) {
			return String.valueOf(((BigDecimal) value).doubleValue());
		} else if (value instanceof Boolean) {
			return ((Boolean) value).booleanValue() ? "1" : "0";
		} else if (value instanceof String) {
			// return ((String) value);
			String string = Jsoup.parse((String) value).text();
			string = "<![CDATA[" + string + "]]>";
			return string;
		}
		return value.toString();
	}

	/**
	 * 根据类型返回对应的SQL TODO://
	 * 
	 * @param type
	 * @param userid
	 * @param lastExportTime
	 * @return
	 */
	protected String getSql(String type, Long userid) {
		if (Contacts.equals(type)) {
			// Contacts的数据从tbuser中导入来的，并且userType=DEAL.
			return "select * from tbuser where baihuiid is null and sellerid="
					+ userid + " and  userType='" + BUser.DEAL + "'";
		} else if (Leads.equals(type)) {
			return "select * from tbuser where  baihuiid is null and sellerid="
					+ userid + " and  userType='" + BUser.UNDEAL + "'";
		} else if (Orders.equals(type)) {
			return "select * from tbtrade where ((exported = 0) or (exported = 2)) and userId="
					+ userid + " ";
		} else if (Products.equals(type)) {
			return "select * from titem where  baihuiid is null and userId="
					+ userid + " ";
		}
		return null;
	}

	/**
	 * TODO://
	 * 
	 * @param map
	 * @param type
	 * @return
	 */
	private Object getTaoBaoId(Map map, String type) {
		if (map == null || map.size() <= 0)
			return null;
		if (Contacts.equals(type) || Leads.equals(type)) {
			return map.get("userId");
		} else if (Orders.equals(type)) {
			return map.get("tid");
		} else if (Products.equals(type)) {
			return map.get("num_iid");
		}
		return null;
	}

	private Map getMapByDoc(Document doc, String type) {
		if (doc == null)
			return null;
		Map resultMap = new HashMap();
		// 得到根元素和Result元素.
		Element root = doc.getRootElement();
		Element ret = root.getChild("result");
		Element typeEl = ret.getChild(type);
		if (ret != null) {
			// 得到Message信息
			List children = typeEl.getChildren("row");
			Element et = null;
			for (int i = 0; i < children.size(); i++) {
				et = (Element) children.get(i);
				List<Element> subElem = et.getChildren("FL");
				String keyString = "";
				String valueString = "";
				for (Element tmp : subElem) {
					String attrValue = tmp.getAttributeValue("val");
					if (attrValue != null) {
						if (Contacts.equals(type) || Leads.equals(type)) {
							if (("Last Name").equals(attrValue)) {
								keyString = tmp.getText();
								break;
							} else if ("CONTACTID".equals(attrValue)) {
								valueString = tmp.getText();
							}
						} else if (Orders.equals(type)) {
							if (("订单编号").equals(attrValue)) {
								keyString = tmp.getText();
								break;
							} else if ("SALESORDERID".equals(attrValue)) {
								valueString = tmp.getText();
							}
						} else if (Products.equals(type)) {
							if (("Product Code").equals(attrValue)) {
								keyString = tmp.getText();
								break;
							} else if ("PRODUCTID".equals(attrValue)) {
								valueString = tmp.getText();
							}

						}
					}
				}
				resultMap.put(keyString, valueString);
			}
		}
		return resultMap;
	}

	/**
	 * 返回updateSQL; TODO://
	 * 
	 * @param baihuiId
	 * @param taobaoId
	 * @param type
	 * @return
	 * @param taobaoObject
	 */
	protected String[] getUpdateSQL(Object baihuiId, Object taobaoId,
			String type, Map taobaoObject) {
		if (taobaoId == null || taobaoId.toString().equals(""))
			return null;
		if (Contacts.equals(type) || Leads.equals(type)) {
			// Contacts的数据从tbuser中导入来的，并且userType=DEAL.
			return new String[] { "update tbuser set exported = 1,baihuiid='"
					+ baihuiId + "' where userid = " + taobaoId.toString()
					+ " and sellerid = " + taobaoObject.get("sellerid") };
		} else if (Orders.equals(type)) {
			String[] updateSqls = new String[1];
			updateSqls[0] = "update tbtrade set exported = 1,baihuiid='"
					+ baihuiId + "' where tid = " + taobaoId.toString()
					+ " and userId = " + taobaoObject.get("userId");

			return updateSqls;
		} else if (Products.equals(type)) {
			String[] updateSqls = new String[1];
			updateSqls[0] = "update titem set exported = 1,baihuiid='"
					+ baihuiId + "' where num_iid = '" + taobaoId.toString()
					+ "'";
			return updateSqls;
		}
		return null;
	}

	/**
	 * 查找最后导出到百会的时间
	 * 
	 * @param type
	 * @param userid
	 * @return
	 * @throws Exception
	 * @deprecated
	 */
	protected Timestamp findLastExportTime(String type, Long userid)
			throws Exception {
		return null;
	}

	/**
	 * 得到对应淘宝模块的DAO.
	 * 
	 * @return
	 */
	public GeneDAO getDomainDAO() {
		return new CommonDAO();
	}

	public String[] getEntryKeys() {
		return null;
	}

	public String getKey(String key) {
		if (key != null && key.indexOf("@") > -1) {
			key = key.replace("@", " ");
		}
		return key;
	}
}
